﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class ketnoi
    {
        // private static string s = ConfigurationManager.ConnectionStrings["ConnectString"].ConnectionString;
        //private static string s = @"Server=(local);UID=nguyentinh;PWD=kimlong;Database=QLBH";
        private static string s = @"Server=(HUNGLE-PC\SQLEXPRESS);Database=QLDT;Trusted_Connection=True;";
        private static SqlConnection connect;
        public ketnoi(string sql)
        {
            s = sql;
        }
        public ketnoi()
        {
            try
            {
                s = @"Server=(HUNGLE-PC\SQLEXPRESS);Database=QLDT;Trusted_Connection=True;";
                connect = new SqlConnection(s);
                connect.Open();
            }
            catch
            { }
        }
        public ketnoi(string server, string name, string pass)
        {
            try
            {
                s = @"Server=" + server + ";UID=" + name + ";PWD=" + pass + ";Database=QLBH";
                connect = new SqlConnection(s);
                connect.Open();
            }
            catch
            { }
        }
        public static SqlConnection Cmb()
        {
            SqlConnection cn = new SqlConnection(s);
            return cn;
        }
        public bool Open()
        {
            try
            {
                connect = new SqlConnection(s);
                connect.Open();
            }
            catch
            {
                return false;
            }
            return true;
        }
        public void Disconnect()
        {
            // if (connect != null)
            {
                connect.Close();
                //MessageBox.Show("Đã đóng kết nối", "Thông báo");
            }
        }
        public static DataTable ExcecuteQuery(string sql)
        {
            try
            {
                SqlConnection cn = new SqlConnection(s);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;
                cmd.CommandText = sql;
                SqlDataAdapter dt = new SqlDataAdapter(cmd);
                DataTable kq = new DataTable();
                dt.Fill(kq);
                cn.Close();
                return kq;
            }
            catch (Exception ex)
            {
                DataTable dt = new DataTable();
                return dt;
            }
        }
        public static DataTable ExcecuteQuery1(string sql)
        {
            try
            {
                SqlConnection cn = new SqlConnection(s);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = sql;
                SqlDataReader daReader = cmd.ExecuteReader();

                DataTable kq = new DataTable();
                kq.Load(daReader);
                cn.Close();
                return kq;
            }
            catch (Exception e)
            {
                DataTable dt = new DataTable();
                return dt;
            }
        }
        /*
         * public static List<HoaDon> select(string maban)
        {
            List<HoaDon> hd = new List<HoaDon>();
            string mahd;
            if (maban != "")
                mahd = layMaHD(maban);
            else mahd = "%";
            string s = "select * from HoaDon where trangthai=0 and mahd like '%"+mahd+"%' order by mahd DESC";
            DataTable dt = ketnoi.ExcecuteQuery(s);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                HoaDon hd1 = new HoaDon();
                hd1.Mahd = dt.Rows[i]["mahd"].ToString();
                hd1.Makm = dt.Rows[i]["makm"].ToString();
                hd1.Manv = dt.Rows[i]["manv"].ToString();
                hd1.Giophut = dt.Rows[i]["giolaphd"].ToString() +":"+ dt.Rows[i]["phutlaphd"].ToString();
                hd1.Ngaythang = dt.Rows[i]["ngay"].ToString() +"-"+ dt.Rows[i]["thang"].ToString() +"-"+ dt.Rows[i]["nam"].ToString();
                hd1.Tiengoc = dt.Rows[i]["tiengoc"].ToString();
                hd1.Tienban = dt.Rows[i]["tienban"].ToString();
                hd1.Trangthai = dt.Rows[i]["trangthai"].ToString();
                hd.Add(hd1);
            }
            return hd;
        }
         */
        public static bool ExcuteNonQuery(string sql)
        {
            try
            {
                SqlConnection cn = new SqlConnection(s);
                cn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
                cn.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static DataTable selectAllImage()
        {
            SqlConnection cn = new SqlConnection(s);
            SqlDataAdapter dda = new SqlDataAdapter("getAllImage", cn);
            dda.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            if (cn.State == ConnectionState.Closed)
            {
                cn.Open();
            }
            dda.Fill(dt);
            cn.Close();
            return dt;
        }

        public static DataTable getImage(string manv)
        {
            SqlConnection cn = new SqlConnection(s);
            SqlDataAdapter dda = new SqlDataAdapter("getImg", cn);
            dda.SelectCommand.Parameters.AddWithValue("@manv", manv);
            dda.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            if (cn.State == ConnectionState.Closed)
            {
                cn.Open();
            }
            dda.Fill(dt);
            cn.Close();
            return dt;
        }
        
    }
}
